package com.xhd.base.obj;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.jar.JarFile;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.cache.CacheMgr;
import com.xhd.cache.UseCache;
import com.xhd.server.ServerConfig;
import com.xhd.xml.XMLSerializer;

public class EnpObjFactory implements UseCache {
	 private HashMap jm15 = new HashMap(1024);
	  private static EnpObjFactory jm16 = null;
	  private static final Log logobj;
	  static Class objclass;
	  static Class EnpobjClassofclas;

	  static
	  {
	    Class tempobjclass = objclass;
	    if (tempobjclass == null)
	    {
	    
	      try
	      {
	        EnpObjFactory.objclass = Class.forName("com.sinocc.base.config.EnpObjFactory");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    logobj = LogFactory.getLog(tempobjclass);
	  }

	  public static synchronized EnpObjFactory getInstance()
	  {
	    if (jm16 == null)
	    {
	      jm16 = new EnpObjFactory();
	      jm16.jm11();
	    }
	    return jm16;
	  }

	  private EnpObjFactory()
	  {
	    CacheMgr.registerCache(this);
	  }

	  public Class getBizClass(String paramString)
	  {
	    if (paramString == null)
	      return null;
	    return ((Class)this.jm15.get(paramString.toLowerCase()));
	  }

	  private void jm11()
	  {
	    try
	    {
	      XMLSerializer localXMLSerializer = null;
	      String str1 = ServerConfig.getConfFilePath("action");
	      File localFile = new File(str1);
	      if ((!(localFile.exists())) && (!(localFile.isFile())))
	        return;
	      FileInputStream localFileInputStream = new FileInputStream(localFile);
	      localXMLSerializer = new XMLSerializer(localFileInputStream, false);
	      Vector localVector = localXMLSerializer.getCollection("pack-config");
	      localFileInputStream.close();
	      if ((localVector == null) || (localVector.size() < 1))
	        return;
	      int i = localVector.size();
	      for (int j = 0; j < i; ++j)
	      {
	        Map localMap = (Map)localVector.get(j);
	        String str2 = (String)localMap.get("pack");
	        String str3 = (String)localMap.get("sample");
	        jm12(str2, str3);
	      }
	      logobj.info("Load biz object class count: " + this.jm15.size());
	    }
	    catch (Exception localException)
	    {
	      logobj.fatal("Load pack config", localException);
	    }
	  }

	  private void jm12(String paramString1, String paramString2)
	  {
	    try
	    {
	      Vector localVector = null;
	      URL localURL = Class.forName(paramString2).getResource("");
	      String str1 = localURL.getPath();
	      logobj.debug("loadBizObjects: " + str1);
	      if ("jar".equalsIgnoreCase(localURL.getProtocol()))
	      {
	        int i = str1.indexOf(33);
	        if (i > 0)
	        {
	          str1 = str1.substring(5, i);
	          localVector = jm13(new JarFile(str1), paramString1);
	        }
	      }
	      else
	      {
	        String str2 = paramString1;
	        int  k = str1.indexOf(str2.replace('.', '/'));
	        if (k > 0)
	        {
	          str1 = str1.substring(0, k + str2.length());
	          localVector = jm13(new File(str1), str2);
	        }
	      }
	      if ((localVector == null) || (localVector.size() <= 0))
	        return;
	      int j = localVector.size();
	      for (int k = 0; k < j; ++k)
	      {
	        Class localClass = (Class)localVector.get(k);
	        String str3 = jm14(localClass.getName());
	        if (this.jm15.containsKey(str3))
	          logobj.warn("Class " + str3 + " repeat!");
	        else
	          this.jm15.put(str3, localClass);
	      }
	    }
	    catch (Throwable localThrowable)
	    {
	      logobj.error("loadBizObjects", localThrowable);
	    }
	  }

	  private Vector jm13(File paramFile, String paramString)
	  {
	    logobj.debug("findBizClasses in: " + paramFile.getPath());
	    Vector localVector1 = new Vector();
	    localVector1.add(paramFile);
	    Class tempobjclas = EnpobjClassofclas;
	    if (tempobjclas == null)
	    {
	      try
	      {
	         EnpObjFactory.EnpobjClassofclas = Class.forName("com.sinocc.base.util.BusinessClass");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    Class localClass1 = tempobjclas;
	    char c = File.separatorChar;
	    String str1 = c + paramString.replace('.', c);
	    Vector localVector2 = new Vector(256);
	    while (localVector1.size() > 0)
	    {
	      paramFile = (File)localVector1.remove(0);
	      File[] arrayOfFile = paramFile.listFiles();
	      if (arrayOfFile == null)
	        continue;
	      String str2 = paramFile.getPath();
	      int i = str2.indexOf(str1);
	      str2 = str2.substring(i + 1);
	      str2 = str2.replace(c, '.');
	      if (str2.equals("com.sinocc.util"))
	        continue;
	      if (str2.equals("com.sinocc.base"))
	        continue;
	      int j = arrayOfFile.length;
	      for (int k = 0; k < j; ++k)
	        if (arrayOfFile[k].isDirectory())
	        {
	          localVector1.add(arrayOfFile[k]);
	        }
	        else
	        {
	          String str3 = arrayOfFile[k].getPath();
	          int l = str3.length();
	          if (l < 7)
	            continue;
	          if (!(".class".equals(str3.substring(l - 6).toLowerCase())))
	            continue;
	          if (str3.indexOf(36) > 0)
	            continue;
	          str3 = str3.substring(0, l - 6);
	          i = str3.indexOf(str1);
	          str3 = str3.substring(i + 1);
	          str3 = str3.replace(c, '.');
	          try
	          {
	            Class localClass2 = Class.forName(str3);
	            if (localClass1.isAssignableFrom(localClass2))
	              localVector2.add(localClass2);
	          }
	          catch (Throwable localThrowable)
	          {
	            if (ServerConfig.IsDebug)
	              logobj.error(str3, localThrowable);
	          }
	        }
	    }
	    return localVector2;
	  }

	  private Vector jm13(JarFile paramJarFile, String paramString)
	  {
	    String str1 = null;
	    Enumeration localEnumeration = paramJarFile.entries();
	    boolean bool = false;
	    Class tempobjclass = EnpobjClassofclas;
	    if (tempobjclass == null)
	    {
	      try
	      {
	        EnpObjFactory.EnpobjClassofclas = Class.forName("com.sinocc.base.util.BusinessClass");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    Class localClass1 = tempobjclass;
	    Vector localVector = new Vector(64);
	    while (localEnumeration.hasMoreElements())
	    {
	      str1 = localEnumeration.nextElement().toString();
	      str1 = str1.replace('/', '.');
	      if (str1.startsWith("com.sinocc.util."))
	        continue;
	      if (str1.startsWith("com.sinocc.base."))
	        continue;
	      if (!(bool))
	      {
	        bool = str1.equals(paramString);
	      }
	      else
	      {
	        if (!(str1.startsWith(paramString)))
	          break;
	        String str2 = str1;
	        int i = str2.length();
	        if (i < 7)
	          continue;
	        if (!(".class".equals(str2.substring(i - 6).toLowerCase())))
	          continue;
	        if (str2.indexOf(36) > 0)
	          continue;
	        str2 = str2.substring(0, i - 6);
	        try
	        {
	          Class localClass2 = Class.forName(str2);
	          if (localClass1.isAssignableFrom(localClass2))
	            localVector.add(localClass2);
	        }
	        catch (Throwable localThrowable)
	        {
	          if (ServerConfig.IsDebug)
	            logobj.error(str2, localThrowable);
	        }
	      }
	    }
	    return localVector;
	  }

	  private String jm14(String paramString)
	  {
	    int i = paramString.lastIndexOf(46);
	    if (i >= 0)
	      paramString = paramString.substring(i + 1);
	    return paramString.toLowerCase();
	  }

	  public boolean clearCache()
	  {
	    this.jm15.clear();
	    System.out.println("EnpObjFactory.clearCache.");
	    jm16 = null;
	    return true;
	  }

	  public int cacheSize()
	  {
	    return this.jm15.size();
	  }

	  public Object cacheData()
	  {
	    return this.jm15;
	  }
}
